﻿Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports BusinessRule.Business
Imports DatabaseAccess.IDAL
Imports DatabaseAccess.DALFactory
Imports BusinessRule.DTO
Imports BusinessRule.IBusiness
Imports External.APICalling
Imports System.Xml

Public Class uMESLoginBusiness
    Implements uMESLoginIBusiness
    'Private dal As OracleDal = New OracleDal()
    Private dal As IOracleDAL = uMESDalContext.instance.GetDalObject()
    Private Encryp As uMES_EncryptionBusiness = New uMES_EncryptionBusiness()

#Region "获取登录员工信息"
    <Ajax.AjaxMethod()> Public Function GetEmployeeByName(ByVal EmployeeName As String) As uMES_AjaxEmployeeDto Implements uMESLoginIBusiness.GetEmployeeByName

        Dim sql As String = <string>SELECT E.*, M.MENUDEFINITIONNAME,MI.PARENTID  FROM EMPLOYEE E
                LEFT JOIN MENUDEFINITION M ON M.MENUDEFINITIONID = E.WEBMENUDEFINITIONID  
                LEFT JOIN (SELECT PARENTID FROM MENUITEM WHERE WORKINGSECTIONURL = 'WorkingSectionMaint.aspx'  GROUP BY PARENTID) MI
                ON MI.PARENTID = E.WEBMENUDEFINITIONID
                    </string>
        sql += String.Format("  where lower(e.employeename)='{0}'", EmployeeName)
        Dim dt As DataTable = dal.GetEmployeeByName(sql)
        Dim dto As uMES_AjaxEmployeeDto = New uMES_AjaxEmployeeDto()

        If dt.Rows.Count > 0 Then
            dto.strEmployeeID = dt.Rows(0)("employeeid").ToString()
            dto.strEmployeeName = dt.Rows(0)("employeename").ToString()
            dto.strCardNum = dt.Rows(0)("cardnum").ToString()
            dto.strPassword = dt.Rows(0)("password").ToString()
            dto.DomainName = dt.Rows(0)("domainname").ToString()
            dto.MenuName = dt.Rows(0)("menudefinitionname").ToString()
            If (dt.Rows(0).IsNull("DomainLogin")) Then
                dto.bolDomainLogin = False
            Else : dto.bolDomainLogin = Convert.ToBoolean(dt.Rows(0)("DomainLogin"))
            End If

            If (dt.Rows(0).IsNull("cardLogin")) Then
                dto.bolCardLogin = False
            Else : dto.bolCardLogin = Convert.ToBoolean(dt.Rows(0)("cardLogin"))
            End If

            If (dt.Rows(0).IsNull("PARENTID")) Then
                dto.HasMaintMenu = False
            Else
                dto.HasMaintMenu = True
            End If

        End If
        Return dto
    End Function

    <Ajax.AjaxMethod()> Public Function GetEmployeeByCardNum(ByVal CardNum As String) As uMES_AjaxEmployeeDto Implements uMESLoginIBusiness.GetEmployeeByCardNum
        Dim sql As String = <string>SELECT E.*, M.MENUDEFINITIONNAME,MI.PARENTID  FROM EMPLOYEE E
                LEFT JOIN MENUDEFINITION M ON M.MENUDEFINITIONID = E.WEBMENUDEFINITIONID  
                LEFT JOIN (SELECT PARENTID FROM MENUITEM WHERE WORKINGSECTIONURL = 'WorkingSectionMaint.aspx'  GROUP BY PARENTID) MI
                ON MI.PARENTID = E.WEBMENUDEFINITIONID
            </string>

        sql += String.Format("  where e.cardnum='{0}'", CardNum)
        Dim dt As DataTable = dal.GetEmployeeByCardNum(sql)
        Dim dto As uMES_AjaxEmployeeDto = New uMES_AjaxEmployeeDto()

        If dt.Rows.Count > 0 Then
            dto.strEmployeeName = dt.Rows(0)("employeename").ToString()
            dto.strFullName = dt.Rows(0)("FullName").ToString
            dto.strCardNum = dt.Rows(0)("cardnum").ToString()
            dto.strPassword = dt.Rows(0)("password").ToString()
            dto.MenuName = dt.Rows(0)("menudefinitionname").ToString()
            dto.DomainName = dt.Rows(0)("domainname").ToString()
            dto.strEmployeeID = dt.Rows(0)("employeeid").ToString() 'add tianFW 2017年12月7日
            If (dt.Rows(0).IsNull("DomainLogin")) Then
                dto.bolDomainLogin = False
            Else : dto.bolDomainLogin = Convert.ToBoolean(dt.Rows(0)("DomainLogin"))
            End If

            If (dt.Rows(0).IsNull("cardLogin")) Then
                dto.bolCardLogin = False
            Else : dto.bolCardLogin = Convert.ToBoolean(dt.Rows(0)("cardLogin"))
            End If

            If (dt.Rows(0).IsNull("PARENTID")) Then
                dto.HasMaintMenu = False
            Else
                dto.HasMaintMenu = True
            End If

        End If
        Return dto
    End Function

    <Ajax.AjaxMethod()> Public Function GetEmployeeByEmployeename(ByVal employeename As String) As uMES_AjaxEmployeeDto Implements uMESLoginIBusiness.GetEmployeeByEmployeename
        Dim sql As String = "select * from employee e where e.employeename='" + employeename + "' "
        Dim dt As DataTable = dal.GetEmployeeByCardNum(sql)
        Dim dto As uMES_AjaxEmployeeDto = New uMES_AjaxEmployeeDto()

        If dt.Rows.Count > 0 Then
            dto.strEmployeeName = dt.Rows(0)("employeename").ToString()
            dto.strFullName = dt.Rows(0)("FullName").ToString
            dto.strCardNum = dt.Rows(0)("cardnum").ToString()
            ' dto.strPassword = Encryp.Decrypto(dt.Rows(0)("password").ToString())
            dto.strPassword = dt.Rows(0)("password").ToString()
            dto.strEmployeeID = dt.Rows(0)("employeeid").ToString() 'add tianFW 2017年12月27日
            dto.DomainName = dt.Rows(0)("domainname").ToString()

            If (dt.Rows(0).IsNull("DomainLogin") = False) Then
                dto.bolDomainLogin = Convert.ToBoolean(dt.Rows(0)("DomainLogin"))
            Else
                dto.bolDomainLogin = False
            End If

            If (dt.Rows(0).IsNull("cardLogin") = False) Then
                dto.bolCardLogin = Convert.ToBoolean(dt.Rows(0)("cardLogin"))
            Else
                dto.bolCardLogin = False
            End If
        End If
        Return dto
    End Function



    Public Function UpdateUserPassword(ByVal Uid As String, ByVal Pwd As String, ByVal expireDate As String) _
        As Integer Implements uMESLoginIBusiness.UpdateUserPassword
        Dim sql As String = "update employee e set e.password='" + Encryp.Encrypto(Pwd) + "'" & vbCrLf & _
            ",e.domainpasswordexpiredate=to_date(" + "'" + expireDate + "','yyyy-mm-dd') where lower(e.employeename)='" + Uid.ToLower() + "'"
        Return dal.UpdateUserPassword(sql)
    End Function

    Public Function UpdateUserPassword(ByVal Uid As String, ByVal Pwd As String) _
     As Integer Implements uMESLoginIBusiness.UpdateUserPassword
        Dim sql As String = "update employee e set e.password='" + Pwd + "'" & vbCrLf & _
            " where lower(e.employeename)='" + Uid.ToLower() + "'"
        Return dal.UpdateUserPassword(sql)
    End Function

    Public Function InsertUserLoginRecord(ByVal dic As Dictionary(Of String, String)) _
        As Integer Implements uMESLoginIBusiness.InsertUserLoginRecord
        Dim sql As String
        sql = "insert into userloginrecord(" & vbCrLf & _
            "clientip,      employeeid,     factoryid,      hostname,       id, " + _
            "loginresult,   notes,          operationdate,  operationtype,  password ) " + _
            " values ('" & vbCrLf & _
            dic("ClientIP") + "','" + dic("EmployeeID") + "','" +
            dic("FactoryID") + "','" + dic("HostName") + "',sys_guid()," + dic("LoginResult") + _
            ",'" + dic("Note") + "',To_Date('" + Now + "','RRRR-MM-DD HH24:MI:SS'), '系统登录', '" + _
            dic("Password") + "' )"
        Return dal.UpdateUserPassword(sql)
    End Function


    Public Function GetDomainUser(ByVal MenuName As String, ByVal Usercount As Dictionary(Of String, Integer), ByRef UserName As String, ByRef Password As String) As Boolean Implements uMESLoginIBusiness.GetDomainUser
        Dim Doc As XmlDocument = New XmlDocument()
        Doc.Load(System.AppDomain.CurrentDomain.BaseDirectory + "\UserAssociate.xml")
        UserName = String.Empty
        Password = String.Empty
        For Each elem As XmlElement In Doc.SelectSingleNode("/Users").ChildNodes
            If elem.Name <> "User" Then
                Continue For
            End If
            If elem.GetAttribute("MenuName") = MenuName Then
                If Usercount.ContainsKey(elem.GetAttribute("UserName")) Then
                    '用户已登录次数判断
                    If Usercount(elem.GetAttribute("UserName")) > Configuration.ConfigurationSettings.AppSettings("UserCount") Then
                        Continue For
                    End If
                End If

                UserName = elem.GetAttribute("UserName")
                Password = elem.GetAttribute("Password")
                Exit For
            End If
        Next
        If String.IsNullOrWhiteSpace(UserName) Then
            GetDomainUser = False
        Else
            GetDomainUser = True
        End If
    End Function


#End Region

End Class
